<?php
/**
 *============================================================================
 * Cornice PHP Framework & Website Builder
 * Freeware (f) 2011 by Inforo Software & Computer
 *============================================================================
 */
 
/**
 * Class used for managing session data
 * @author Nicola Dinh
 * @version 2011.04.2702
 */
class session {

  /**
   * Default instance level constructor
   * Just decorative, should not be called due to having only static methods
   */
  public function __construct() {
    //
  }
  
  /**
   * Initialise the session (used for both starting or resuming)
   */
  public static function initialise() {
    $lf_on_server = config::get_config_value("session/lifetime/server");
    $lf_of_cookie = config::get_config_value("session/lifetime/cookie");
    $module_name  = request::get_module_name();
  
    //set ini
    ini_set("session.gc_maxlifetime", $lf_on_server);  
    ini_set("session.cookie_lifetime",$lf_of_cookie);
    session_name($module_name."_sid");
    session_start();    

    //update session cookie lifetime 
    if (strlen(session_id())>0)
      setcookie(session_name(),session_id(),time()+$lf_of_cookie,"/");
  }
  
  /**
   * Destroy session of current visitor
   */
  public static function destroy() {
    session_destroy();
  }
   
  /**
   * Get value of a certain session variable
   * @param string $var_name The name of session variable
   * @return mixed The session variable
   */
  public static function get_value($var_name) {
    return isset($_SESSION[$var_name])? $_SESSION[$var_name]:null;
  }
  
  /**
   * Clear a session variable
   * @param string $var_name The name of session variable
   */
  public static function clear_value($var_name) {
    if (isset($_SESSION[$var_name]))
      unset($_SESSION[$var_name]);
  }
  
  /**
   * Set a session variable
   * @param string $var_name The name of session variable
   * @param mixed $var_value The value of session variable to set
   */
  public static function set_value($var_name,$var_value) {
    $_SESSION[$var_name] = $var_value;
  }
}